const WXAPI = require('apifm-wxapi')


const TOOLS = require('../../utils/tools.js')
const AUTH = require('../../utils/auth')
import Poster from 'wxa-plugin-canvas/poster/poster'
const APP = getApp()
// 在页面中定义插屏广告
let interstitialAd = null


Page({
  data: {
    sjc: Date.parse(new Date()),
    topNum: 100,
    bnavHeight: APP.globalData.bnavHeight,
    navHeight: APP.globalData.navHeight,
    xx: "",
    lcsj: "",
    xgcp: "",
    info: "",
    aislogo: true,
    cl_open: false,
    wzcl: undefined,
    nodeStyle: {
      'h3': 'margin:30px 0;'
    },
    dm: false,
  },

  _cl_open() {
    this.setData({
      cl_open: !this.data.cl_open
    })
  },
  toliebiao(e) {
    wx.vibrateShort();
    const tag_id = e.currentTarget.dataset.id;
    if (tag_id.indexOf("http") == 0) {
      wx.navigateTo({
        url: `/pages/h5/index?id=${tag_id}`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/taglist/index?id=${tag_id}`,
      })
    }
  },

  fanhui() {
    wx.navigateBack()
  },


  tapBanner: function (e) {
    const url = e.currentTarget.dataset.url
    if (url) {
      wx.navigateTo({
        url
      })
    }
  },

  tovip(e) {
    let id = e.currentTarget.dataset.id
    console.log("sssssssssssss", id);
    wx.setStorageSync('vipid', id);

    wx.switchTab({
      url: '/pages/coupons/hy',
    })
  },


  onLoad: function (e) {

    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-b8fb76c1d787db57'
      })
      interstitialAd.onLoad(() => { })
      interstitialAd.onError((err) => { })
      interstitialAd.onClose(() => { })
    }

    if(e.dm){
      this.setData({
        dm: e.dm,
      })
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }




    console.log("onLoad:", e);
    // 加载
    if (e.id) {
      console.log("id:", e.id);
      this.setData({
        wzid: e.id
      })
    }





    const that = this;

    if (e && e.scene) {
      const scene = decodeURIComponent(e.scene) // 处理扫码进商品详情页面的逻辑

      e.id = scene.split(',')[0]
      let efxz = scene.split(',')[1]
      console.log("efxz", e.id, efxz);

      that.setData({
        wzid: e.id,
        fxz: efxz,
      })

    }


    // that.initBanners()

    that.adPosition2(e.id);

  },

  toshangping(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/goods-details/index?id=${id}`,
    })

  },

  toxiangqing(e) {
    wx.vibrateShort();
    let tag_id = e.currentTarget.dataset.id;
    wx.redirectTo({
      url: `/pages/xiangqing/index?id=${tag_id}`,
    })
  },

  jdbgg() {
    wx.navigateTo({
      url: '/pages/jdbgg/index',
    })
  },

  _wzcl(e) {
    let wzid = e.currentTarget.dataset.id;
    this.setData({
      wzid: wzid,
      cl_open: false,
    })
    this.adPosition2(wzid);

  },
  close_cl_pop() {
    this.setData({
      cl_open: false,
    })
  },

  parseHtml(html) {
    // 这里需要使用专门的HTML解析库，例如htmlparser2
    // 处理过程略，最终返回处理后的nodes对象
    return nodes.map(node => {
      if (node.name === 'h3') {
        node.attrs.style = 'margin:30px 0';
      }
      return node;
    });
  },
  async adPosition2(a) {
    let res = await WXAPI.cmsArticleDetailV2(a);
    // let keywords = res.data.info.keywords;
    if (res.data.info.tags) {
      let keywords = res.data.info.tags;
      keywords = keywords.split("、");
      this.setData({

        keywords: keywords,
        tbtag: keywords[0]
      })
      this.lc1(keywords[0]);

    }


    let info = res.data.info.content;
    info = info.replace(/\<img/g, "<img style='max-width: 100%;width:auto;height:auto;margin:10px 0;float:left;display:block '")

    info = info.replace(/\<h3/g, "<h3 style='margin:20px 0;'")
    info = info.replace(/\<h2/g, "<h2 style='margin:20px 0;'")
    info = info.replace(/\<h4/g, "<h4 style='margin:20px 0;'")
    info = info.replace(/\<ul/g, "<ul style='margin:20px 0;'")
    info = info.replace(/\<ol/g, "<ol style='margin:20px 0;'")

    this.setData({
      xx: res.data,
      info: info
    })



    console.log("xx", this.data.xx);
    if (this.data.xx.extJson["自定义广告ID"]) {
      this.ad(this.data.xx.extJson["自定义广告ID"]);
    } else {
      this.ad("默认广告不要删除");
    }


    if (this.data.xx.extJson["wzcl"]) {
      const str = this.data.xx.extJson["wzcl"];

      const result = str
        .split(";")
        .filter(Boolean)
        .map((item) => {
          const [name, id] = item.split(",");
          return {
            name: name.trim(),
            id: Number(id.trim()),
          };
        });
      this.setData({
        wzcl: result,
      });

      console.log("wzcl", this.data.wzcl);
    } else {
      this.setData({
        wzcl: undefined,
      });

    }




    this.tag(this.data.tbtag);



  },

  async ad(a) {
    console.log(a);

    let res = await WXAPI.cmsPage(a)

    this.setData({
      ad: res.data.extJson,
    })
    console.log("ad", this.data.ad);
  },

  async tag(a) {

    let res = await WXAPI.cmsPage(a)

    if (res.data) {
      this.setData({
        topbj: res.data.extJson.topimg,
        tagid: a,
      })
      console.log("tag", this.data.xx);
    }

  },
  isShow: function (ls, jj) {

    let a = false
    let lsa = ls.split('、')

    let that = this
    lsa.forEach(function (e) {
      if (e.toLowerCase() == (jj).toLowerCase()) {
        a = true
      }
    })
    return a
  },

  async lc1(a) {
    let res = await WXAPI.cmsArticles({
      keywordsLike: a,
      pageSize: 20
    })

    if (res.data.result) {


      let temp = []
      let sj = res.data.result
      sj.forEach(element => {


        if (element.video) {
          let av = element.video

          av = av.split(",")
          if (av[0]) {
            element.video1 = av[0];
          }
          if (av[1]) {
            element.video2 = av[1];
          }
          if (av[2]) {
            element.video3 = av[2];
          }




          // images[i].video = av;
        }

        if (this.isShow(element.keywords, a)) {
          temp.push(element)
        }
      });


      this.setData({
        lcsj: temp,
      })
      console.log("lcsj", this.data.lcsj);
    }


  },



  toliebiao(e) {

    let tag_id = e.currentTarget.dataset.id;
    if (tag_id.indexOf("http") == 0) {
      let site = tag_id.lastIndexOf("\/");
      tag_id = tag_id.substring(site + 1, tag_id.length);
    }

    wx.vibrateShort();


    console.log(tag_id);
    wx.navigateTo({
      url: `/pages/taglist/index?id=${tag_id}`,
    })

  },

  // async toxiangqing(e){



  //   wx.vibrateShort();

  //   const tag_id = e.currentTarget.dataset.ids;

  //   wx.navigateTo({
  //     url: `/pages/xiangqing/index?id=${tag_id}`,
  //   })

  // },






  onShow: function (e) {
    this.setData({
      navHeight: APP.globalData.navHeight,
      navTop: APP.globalData.navTop,
      windowHeight: APP.globalData.windowHeight,
      menuButtonObject: APP.globalData.menuButtonObject //小程序胶囊信息
    })

    var _this = this;
    if (typeof _this.getTabBar === 'function' &&
      _this.getTabBar()) {
      _this.getTabBar().setData({
        selected: 0
      })
    }
  },



  onPageScroll(e) {
    let scrollTop = this.data.scrollTop
    this.setData({
      scrollTop: e.scrollTop
    })
  },


  onShareTimeline: function () {
    return {
      title: this.data.xx.info.title,
      imageUrl: this.data.xx.info.address,
      // title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
      query: 'id=' + this.data.wzid+'&dm=' + this.data.dm,
    }
   },

  onShareAppMessage: function () {
    return {
      title: this.data.xx.info.title,
      imageUrl: this.data.xx.info.address,
      // title: '"' + wx.getStorageSync('mallName') + '" ' + wx.getStorageSync('share_profile'),
      // wx.getStorageSync('uid')
      path: '/pages/xiangqing/index?id=' + this.data.wzid+'&dm=' + this.data.dm,
    }
  },













  closePop() {
    this.setData({
      posterShow: false
    })
  },
  async drawSharePic() {
    const _this = this
    console.log("_this.data.xx.info.id", _this.data.xx.info.id + ',' + wx.getStorageSync('uid'));
    const qrcodeRes = await WXAPI.wxaQrcode({
      scene: _this.data.xx.info.id + ',' + wx.getStorageSync('uid'),
      page: 'pages/xiangqing/index',
      is_hyaline: true,
      autoColor: true,
      expireHours: 1
    })
    if (qrcodeRes.code != 0) {
      wx.showToast({
        title: qrcodeRes.msg,
        icon: 'none'
      })
      return
    }
    console.log("qrcodeRes", qrcodeRes);
    const qrcode = qrcodeRes.data
    const pic = _this.data.xx.info.pic?_this.data.xx.info.pic:_this.data.xx.info.address
    console.log("fxpic",pic);
    wx.getImageInfo({
      src: pic,
      success(res) {
        const height = 490 * res.height / res.width
        _this.drawSharePicDone(height, qrcode)
      },
      fail(e) {
        console.error(e)
      }
    })
  },
  drawSharePicDone(picHeight, qrcode) {
    console.log("picHeight, qrcode", picHeight, qrcode);
    const _this = this
    const _baseHeight = 74 + (picHeight + 120)
    this.setData({
      posterConfig: {
        width: 750,
        height: picHeight + 660,
        backgroundColor: '#fff',
        debug: false,
        blocks: [{
          x: 76,
          y: 74,
          width: 604,
          height: picHeight + 120,
          borderWidth: 2,
          borderColor: '#c2aa85',
          borderRadius: 8
        }],
        images: [{
          x: 133,
          y: 133,
          url: _this.data.xx.info.pic?_this.data.xx.info.pic:_this.data.xx.info.address, // 商品图片
          width: 490,
          height: picHeight
        },
        {
          x: 76,
          y: _baseHeight + 199,
          url: qrcode, // 二维码
          width: 222,
          height: 222
        }
        ],
        texts: [{
          x: 375,
          y: _baseHeight + 80,
          width: 650,
          lineNum: 2,
          text: _this.data.xx.info.title,
          textAlign: 'center',
          fontSize: 40,
          color: '#333'
        },
        // {
        //   x: 375,
        //   y: _baseHeight + 180,
        //   text: '￥' + _this.data.goodsDetail.basicInfo.minPrice,
        //   textAlign: 'center',
        //   fontSize: 50,
        //   color: '#e64340'
        // },
        {
          x: 352,
          y: _baseHeight + 320,
          text: '长按识别小程序码',
          fontSize: 28,
          color: '#999'
        }
        ],
      }
    }, () => {
      Poster.create();
    });
  },
  onPosterSuccess(e) {
    console.log('success:', e)
    this.setData({
      posterImg: e.detail,
      showposterImg: true
    })
  },
  onPosterFail(e) {
    console.error('fail:', e)
  },
  savePosterPic() {
    const _this = this
    wx.saveImageToPhotosAlbum({
      filePath: this.data.posterImg,
      success: (res) => {
        wx.showModal({
          content: '已保存到手机相册',
          showCancel: false,
          confirmText: '知道了',
          confirmColor: '#333'
        })
      },
      complete: () => {
        _this.setData({
          showposterImg: false
        })
      },
      fail: (res) => {
        wx.showToast({
          title: res.errMsg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },



})